package com.fg.pro.ai.AiController;


import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import com.alibaba.cloud.ai.dashscope.image.DashScopeImageModel;
import com.alibaba.cloud.ai.dashscope.image.DashScopeImageOptions;
import com.fg.pro.model.reponse.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "aiAlibaba",description = "ai_alibaba")
@RestController
@RequestMapping("aiAlibaba")
public class AiAlibabaController {

    @Autowired
    DashScopeChatModel dashScopeChatModel;

    @Autowired
    DashScopeImageModel dashScopeImageModel;


    @Operation(summary = "阻塞模式回答",description = "")
    @GetMapping("/chartByBlock")
    public ResponseResult chartByBlock(@RequestParam(required = false)String question){
        String call = dashScopeChatModel.call(question);
        System.out.println(call);
        return ResponseResult.okResult(call);
    }

    @Operation(summary = "图片生成",description = "")
    @GetMapping("/imgByBlock")
    public ResponseResult imgByBlock(@RequestParam(required = false)String question){
        DashScopeImageOptions imageOptions = DashScopeImageOptions
                .builder()
                .withModel("qwen-image")
                .build();
        ImageResponse response = dashScopeImageModel.call(new ImagePrompt(question, imageOptions));
        System.out.println(response);
        // 图片url
        String url = response.getResult().getOutput().getUrl();
        // 图片base64
        String b64Json = response.getResult().getOutput().getB64Json();
        return ResponseResult.okResult(url);
    }

    @Operation(summary = "默认回答(记忆体)",description = "")
    @GetMapping("/chartByMemory")
    public ResponseResult chartByMemory(){
        MessageWindowChatMemory chatMemory = MessageWindowChatMemory.builder().build();
        String conversationId = "x001";
        UserMessage userMessage1 = new UserMessage("我是籍祥");
        chatMemory.add(conversationId,userMessage1);
        ChatResponse response1 = dashScopeChatModel.call(new Prompt(chatMemory.get(conversationId)));
        chatMemory.add(conversationId,response1.getResult().getOutput());

        UserMessage userMessage2 = new UserMessage("我叫什么");
        chatMemory.add(conversationId,userMessage2);
        ChatResponse response2 = dashScopeChatModel.call(new Prompt(chatMemory.get(conversationId)));
        chatMemory.add(conversationId,response2.getResult().getOutput());


        return ResponseResult.okResult(response2.getResult().getOutput());
    }

}
